package yue.shen.android.demo

import android.os.Bundle
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import yue.shen.android.demo.databinding.ActivityCoroutiesScope1Binding
import kotlin.concurrent.thread

class CoroutineScopeActivity1 : AppCompatActivity() {

    private lateinit var viewBinding: ActivityCoroutiesScope1Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityCoroutiesScope1Binding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        initData()
    }

    private fun initData() {

//        GlobalScope.launch {
//            println("Coroutines Camp 1 ${Thread.currentThread().name}")
//        }
//
//        thread {
//            println("Coroutines Camp 2 ${Thread.currentThread().name}")
//        }

        GlobalScope.launch(Dispatchers.Main) {
            ioCode1()
            uiCode1()
        }


//        ioCode2()
//        uiCode2()
//
//        ioCode3()
//        uiCode3()
    }

    private suspend fun ioCode1() {
        withContext(Dispatchers.IO) {
            SystemClock.sleep(5000)
            println("Coroutines Camp ioCode1 ${Thread.currentThread().name}")
        }
    }

    private suspend fun ioCode2() {
        println("Coroutines Camp ioCode2 ${Thread.currentThread().name}")
    }

    private suspend fun ioCode3() {
        println("Coroutines Camp ioCode3 ${Thread.currentThread().name}")
    }

    private fun uiCode1() {
        println("Coroutines Camp uiCode1 ${Thread.currentThread().name}")
    }

    private fun uiCode2() {
        println("Coroutines Camp uiCode2 ${Thread.currentThread().name}")
    }


    private fun uiCode3() {
        println("Coroutines Camp uiCode3 ${Thread.currentThread().name}")
    }


}